#!/bin/bash

xdefine ( )
{
  #获取新图像文件路径
	#txtfile=`ls $PATHIMLIS`
	txtfile_pwd=`echo $PATHIMLIS`
	sT=`date -u +%Y%m%d`
	#sT="20230818"
	Newimgtxtfile=`echo $sT | awk '{print("imlis_"$1".txt")}'`
	Newimglist=`echo $txtfile_pwd $sT | awk '{print($1"imlis_"$2".txt")}'`
	echo $Newimglist

	
	#stringYearForMonitorT=`date -u +%Y`
	#stringtimeForMonitorT=`date -u +%Y-%m-%d`
	#Dir_rawdata=`echo "$HOME/data/Y$stringYearForMonitorT/$stringtimeForMonitorT/mini-GWAC"`
	
	stringtimeForMonitorT=`date -u +%Y%m%d`
	dir_reduction=/disk2/test/xlp2/svom/$stringtimeForMonitorT
	cd /disk2/test/xlp2/svom
	if test ! -d $stringtimeForMonitorT
	then		
		echo "making $stringtimeForMonitorT"
		mkdir $stringtimeForMonitorT
  fi
	cd $dir_reduction
  #xprocess_old_file
}

xprocess_old_file ( )
{
  echo "Reduce the fits in the history list [ Y/N ]:"
	read AnswerWords
	AnswerWords='N'
	if [ "$AnswerWords"x == "Y"x ]
	then
	    echo "OK, these fit images will be reduced one by one"
	    sleep 1
	else
	    cat $Newimglist | column -t | awk '{print($1"/"$2)}' >oldlist

	fi
}


#echo "bias dark flat focus light" | xargs -n 1 >listdatafile 


xobjectreduction (  )
{
    fitname=$1
    echo $fitname
    xflatcheckandcombine 
    sh /home/cgft/software/softgft_mainFocus/xgft_ot.sh $fitname
    #sh /home/cgft/software/softgft/xgft_ot.sh $fitname
}
xbiascheckandcombine (  )
{
    :
}
xdarkcheckandcombine (  )
{
    :
}
xflatcheckandcombine (  )
{
    dir_now=`pwd`
    dirFlat="/disk2/test/xlp2/svom/caliFile/Flat"
    #dirFlatg="/disk2/test/xlp2/svom/caliFile/Flat_g.fit"
    #dirFlati="/disk2/test/xlp2/svom/caliFile/Flat_i.fit"
    dirZero="/disk2/test/xlp2/svom/caliFile/Zero.fit"
    filter=`gethead $fitname "filter"`
    flatnamedir=`echo $dirFlat"_"$filter".fit"`
    if [ -r $flatnamedir ] && [ -r $dirZero  ]
    then
	    cp $flatnamedir $dirZero ./
	    flatname=`echo "Flat_"$filter".fit"`
	    xzeroflatcorr.sh $fitname $flatname
    fi
}

xcopyobslist (  )
{
   # echo "xcopyobslist"
    if test ! -r oldlist
    then
        touch oldlist
    fi
    if test -s $Newimglist 
    then
        cp $Newimglist $dir_reduction
        # gft_observed.list is copied
        #cat gft_observed.list | awk '{print($1"/"$2)}' >newlist
        cat $Newimgtxtfile | column -t | awk '{print($1"/"$2)}' >newlist
        diff oldlist newlist | grep ">" | tr -d '>' | column -t  >listmatch
        linedata=`cat listmatch | wc -l`
        if [ "$linedata" -ne 0  ]
        then
            echo "new image exits"
            cat listmatch | while read linedataroad
            do
                N_dataline=`echo $linedataroad | wc | awk '{print($2)}'`
                echo $linedataroad
                echo $N_dataline
                if [ $N_dataline -lt 1   ]
                then
                    echo "The information of this fits is not correct"
                else
                   # pathroad=`echo $linedataroad | awk '{print($1)}'`
                   # fitsname=`echo $linedataroad | awk '{print($2)}'`
                   # cp $pathroad"/"$fitsname $dir_reduction
                  cp $linedataroad $dir_reduction
                  fitsname=`echo $linedataroad | sed 's/\// /g' | awk '{print($NF)}'`
                  echo $linedataroad >>oldlist
                  echo "fitsname="$fitsname
                  #echo "$pathroad $fitsname" | column -t   >>oldlist
                  gethead $fitsname "TRIG_RA" >tmp
                  if test -s tmp
                  then
                    xobjectreduction  $fitsname
                  fi
		            fi
            done
        else
            #echo "No new image exist"
            sleep 1
            #continue
        fi 
    else
        #echo "No any data"
        sleep 1
        #continue
    fi

}

xcheckdatafile (  )
{

    xcopyobslist


    #for datafilename in `cat listotnamefile`
    #do
    #   Ab_datafilename=`echo $Dir_rawdata"/"$datafilename`
    #   echo $Ab_datafilename
    #   cd $Ab_datafilename
    #   xchecknewimage
    #done

}

while :
do
    xdefine
    cd $dir_reduction
    xcopyobslist
    #xcheckdatafile
done
